package com.probiz.estore.catalog.service.impl;

import java.util.ArrayList;
import java.util.List;

import com.probiz.estore.common.model.catalog.VirtualCategory;
import com.probiz.estore.common.model.catalog.VirtualcatProdRelation;
import com.probiz.estore.common.model.sales.RecommendedProduct;
import com.probiz.estore.core.service.impl.GenericManagerImpl;
import com.probiz.estore.catalog.service.VirtualcatProdRelationManager;
import com.probiz.estore.catalog.dao.VirtualcatProdRelationDao;
import com.probiz.estore.sales.SalesConstants;


/**
 * Manager implementation for VirtualcatProdRelation, responsible for business processing, and communicate between web and persistence layer.
 */
public class VirtualcatProdRelationManagerImpl extends GenericManagerImpl<VirtualcatProdRelation> implements VirtualcatProdRelationManager {

	private VirtualcatProdRelationDao virtualcatProdRelationDao = null;

	/**
	 * @param virtualcatProdRelationDao
	 *            the virtualcatProdRelationDao to set
	 */
	public void setVirtualcatProdRelationDao(VirtualcatProdRelationDao virtualcatProdRelationDao) {
		this.virtualcatProdRelationDao = virtualcatProdRelationDao;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.probiz.estore.core.service.impl.GenericManagerImpl#init()
	 */
	@Override
	protected void initManager() {
		dao = virtualcatProdRelationDao;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.probiz.estore.core.service.impl.GenericManagerImpl#onDelete(java.lang.Object)
	 */
	@Override
	protected void preDelete(VirtualcatProdRelation entity) {

	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.probiz.estore.core.service.impl.GenericManagerImpl#onSave(java.lang.Object)
	 */
	@Override
	protected void preSave(VirtualcatProdRelation entity) {

	}
	/*
	 * (non-Javadoc)
	 * @see com.probiz.estore.catalog.service.VirtualcatProdRelationManager#saveRecommendedProductsFromArrays(com.probiz.estore.common.model.catalog.VirtualCategory, java.lang.String[])
	 */
	public void saveRecommendedProductsFromArrays(VirtualCategory category,String[] productIds){
		List<VirtualcatProdRelation> formerVprList = new ArrayList<VirtualcatProdRelation>(); //必须用这种方法 ArrayList支持remove方法
		int sourceKind;
		Integer sourceId;
		//原来的推荐商品
		for(VirtualcatProdRelation formerVpr : category.getVirtualcatProdRelations()){
			formerVprList.add(formerVpr);
		}
		
		
		for(int i=0; i< productIds.length; i++){
			VirtualcatProdRelation vpr = virtualcatProdRelationDao.getVirtualcatProdRelation(category.getCategoryId(), Integer.parseInt(productIds[i]));
			if(null != vpr){
				//修改
				vpr.setSortOrder(i);
				if(formerVprList.contains(vpr)){
					formerVprList.remove(vpr);
				}
			}else{
				//新增
				vpr = new VirtualcatProdRelation();
				vpr.setVirtualCategory(category);
				vpr.setProductId(Integer.parseInt(productIds[i]));
				vpr.setSortOrder(i);
			}
			virtualcatProdRelationDao.save(vpr);
		}	
		
		//删除
		for(VirtualcatProdRelation vpr: formerVprList){
			virtualcatProdRelationDao.delete(vpr);
		}
	}

}
